<template>
  <Dialog :appDialog="appDialog" key="proAppDialog">
    <div class="content">
      <el-form ref="form" :model="protecform" label-width="100px"
        ><el-form-item label="客户保护设置"> <el-switch v-model="protecform.is_enable_protect"></el-switch> </el-form-item>
        <el-form-item> <div class="tips">开启后，员工可在企微客易达应用为客户设置保护，开启了保护的客户将不会自动回收公海或者被其他员工认领。</div></el-form-item>
        <template v-if="protecform.is_enable_protect"
          ><el-form-item label="客户保护数量"><el-input-number v-model="protecform.number" ref="inputRef" controls-position="right" :min="1"></el-input-number></el-form-item
        ></template> </el-form
    ></div>
  </Dialog>
  <Dialog :appDialog="tips">
    <div class="dialog_content">
      <span>您还有{{ num }}个设置保护客户的名额，设为保护后，客户将不会被系统回收。您确定要将客户该客户设为保护吗？</span></div
    >
  </Dialog>
</template>
<script setup lang="ts">
  import Dialog from '@/components/Dialog.vue'
  import { reactive } from 'vue'
  import { ElMessageBox, ElMessage } from 'element-plus'
  import { guestKeep, delWorkUser, beforeKeep } from '@/api/employe/employeemanage/manage'
  const emit = defineEmits({
    refreshTable: null,
  })

  const cancelHandel = (value: any) => {
    appDialog.visible = false
  }
  const submitHandel = (val: any) => {
    if (protecform.number <= 5) {
      guestKeep(protecform).then((res: any) => {
        ElMessage({ message: '客户保护设置成功', type: 'success' })
        emit('refreshTable')
        appDialog.visible = false
      })
    } else {
      ElMessage.error('客户保护数量小于或等于5个')
    }
  }
  const tipscancel = (value: any) => {
    tips.visible = false
  }
  const tipsubmit = (data: any) => {
    if (data.is_enable_protect) {
      protecform.is_enable_protect = true
      protecform.number = data.protect_max_num
    } else {
      protecform.is_enable_protect = false
      protecform.number = 0
    }
    protecform.id = data.id
    appDialog.title = `员工【${data.name}】客户保护设置`
    appDialog.visible = true
  }

  const tips = reactive({
    visible: false,
    title: '客户保护',
    width: '450px',
    cancel: tipscancel,
    submit: tipsubmit,
    cancelText: '取消',
    submitText: '确定'
  })
  const appDialog = reactive({
    visible: false,
    title: '员工【名称】客户保护设置',
    width: '450px',
    cancel: cancelHandel,
    submit: submitHandel,
    cancelText: '取消',
    submitText: '确定'
  })
  let protecform = reactive({
    id: '', //ID
    number: 0, //设置保护的客户数
    is_enable_protect: false
  })

  const del = (data: any) => {
    const params = {
      id: data.id
    }
    ElMessageBox({
      title: '提示',
      message: `您确定要删除员工【${data.name}】吗?`,
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      showCancelButton: true,
      type: 'warning'
    })
      .then(() => {
        delWorkUser(params).then((data: any) => {
          ElMessage({
            message: '删除成功',
            type: 'success'
          })
          emit('refreshTable')
        })
      })
      .catch(() => {})
  }
  // 客户保护预请求
  const prekeep = (id: any) => {
    return beforeKeep({ id })
  }
  //保护客户
  const protect = async (data: any) => {
    protecform.id = data.id
    var res = await prekeep(data.id)
    if (res.is_protected) {
      tips.title = '取消保护'
    } else {
      tips.title = '取消'
    }
    tips.visible = true
  }

  defineExpose({ del, protect, tipsubmit })
</script>
<style lang="scss" scoped>
  .content {
    .tips {
      padding: 12px;
      margin-bottom: 16px;
      background: #fffaf9;
      font-size: 12px;
      color: #585858;
      line-height: 20px;
    }
  }
  .isMale {
    width: 18px;
    line-height: 18px;
    border-radius: 50%;
    display: inline-block;
    color: #ffffff;
    background-color: #75ddf4;
    text-align: center;
  }
  .isMales {
    width: 18px;
    line-height: 18px;
    border-radius: 50%;
    display: inline-block;
    color: #ffffff;
    background-color: #eaeaea;
    text-align: center;
  }
  .isfeMale {
    background-color: #f474bf;
  }
  .form-tags {
    margin: 0 10px 10px 0;
  }
  .edit-btn {
    margin-right: 10px;
  }
  .form-dropdown {
    line-height: 30px;
    padding: 0 15px;
  }
  .empty {
    height: 400px;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    .emty_img {
      transform: translateX(15%);
    }
    .emty_title {
      margin-top: -25px;
    }
  }
  .zw-table {
    min-height: 450px;
    &__card-header {
      margin-bottom: 10px;
      justify-content: flex-end;
    }
    .el-table {
      .el-table__header-wrapper {
        .el-table__header {
          thead {
            tr {
              th {
                background-color: #f9f9f9;
              }
            }
          }
        }
      }

      .el-table__cell {
        position: relative;

        // .el-tooltip {
        //   @include text-ellipsis(2);
        // }
      }
      .el-tag {
        margin-right: 6px;
      }
    }
    .el-pagination {
      padding: 0;
      margin-top: 10px;
      text-align: right;

      .el-pagination__sizes {
        margin-right: 0;
      }
    }

    &__btn-box {
      margin-bottom: -8px;
      text-align: left;
      overflow: hidden;

      .el-button {
        cursor: pointer;
        display: inline-block;
        vertical-align: middle;
        width: 80px;
        height: 24px;
        margin: 0 4px 8px;
        border: 1px solid #eaeaea;
        border-radius: 2px;
        text-align: center;
        background: #fff;
      }
    }
  }
  .tag_box {
    display: flex;
    display: -webkit-box; //谷歌
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 1; //显示几行
    overflow: hidden;
    .form-tags {
      margin: 0px 10px 0px 0px;
    }
  }
</style>
